package com.inlaylink.cordova.plugin.alipay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaPreferences;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;


import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.response.*;
import com.alipay.sdk.app.PayTask;
import com.google.gson.Gson;
import com.alipay.api.request.*;


public class Alipay extends CordovaPlugin{
	public static  String partner;
	public static String appid;
	public static String rsa_private;
	public static String rsa_public;
	public static String gateway;
  public static String outTradeNo;
  public static String tradeNo;
  final String format = "json";
  final String charset = "utf-8";
  final String signtype = "RSA";


	private static final int SDK_PAY_FLAG = 1;
	private static final int SDK_CHECK_FLAG = 2;
	private static final int SDK_QRPAY_FLAG = 3;
  private static final int SDK_TRADE_QUERY_FLAG = 4;

  private static final String GATEWAY = "https://openapi.alipay.com/gateway.do";

	CallbackContext currentCallbackContext;

	@Override
	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {
		// save the current callback context
		currentCallbackContext = callbackContext;
		// app支付
		if (action.equals("pay")) {
			return pay(args);
		}
		// 二维码支付
		if (action.equals("qrpay")) {
			// 调用支付宝接口返回二维码
			return qrpay(args);
		}

    //统一收单线下交易查询
    if (action.equals("tradeQuery")) {
      return tradeQuery(args);
    }

		return true;
	}

	@Override
	public void initialize(CordovaInterface cordova, CordovaWebView webView) {
		super.initialize(cordova, webView);

	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SDK_PAY_FLAG: {
				Result resultObj = new Result((String)msg.obj);
				String resultStatus = resultObj.resultStatus;

				currentCallbackContext.success(resultStatus);

//				// 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
//				if (TextUtils.equals(resultStatus, "9000")) {
//
//					Toast.makeText(cordova.getActivity(), "支付成功",
//							Toast.LENGTH_SHORT).show();
//				} else {
//					// 判断resultStatus 为非“9000”则代表可能支付失败
//					// “8000” 代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
//					if (TextUtils.equals(resultStatus, "8000")) {
//						Toast.makeText(cordova.getActivity(), "支付结果确认中",
//								Toast.LENGTH_SHORT).show();
//
//					} else {
//						Toast.makeText(cordova.getActivity(), "支付失败",
//								Toast.LENGTH_SHORT).show();
//
//					}
//				}
				break;
			}
			case SDK_CHECK_FLAG: {
				Toast.makeText(cordova.getActivity(), "检查结果为：" + msg.obj,
						Toast.LENGTH_SHORT).show();
				break;
			}
			case SDK_QRPAY_FLAG: {
				currentCallbackContext.success((String) msg.obj);
				break;
			}

      case SDK_TRADE_QUERY_FLAG: {
          currentCallbackContext.success((String) msg.obj);
          break;
        }
			default:
				break;
			}
		};
	};



	/**
	 * call alipay sdk pay. 调用SDK支付
	 *
	 */
	private  boolean pay(CordovaArgs args) {
		try {
			JSONObject orderInfoArgs =  args.getJSONObject(0);
			String subject = orderInfoArgs.getString("subject");
			String body = orderInfoArgs.getString("body");
			String price = orderInfoArgs.getString("price");
			String tradeNo = orderInfoArgs.getString("tradeNo");
			String timeout = orderInfoArgs.getString("timeout");
			String notifyUrl = orderInfoArgs.getString("notifyUrl");
			String seller = orderInfoArgs.getString("seller");

			partner = orderInfoArgs.optString("partner", "");
			rsa_private = orderInfoArgs.optString("rsa_private", "");
			rsa_public = orderInfoArgs.optString("rsa_public", "");

			//"测试的商品", "该测试商品的详细描述", "0.01","30m","www.justep.com"
			String orderInfo = getOrderInfo(seller,subject,body,price,tradeNo,timeout,notifyUrl);
			String sign = sign(orderInfo);
			try {
				// 仅需对sign 做URL编码
				sign = URLEncoder.encode(sign, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			final String payInfo = orderInfo + "&sign=\"" + sign + "\"&"
					+ getSignType();

			Runnable payRunnable = new Runnable() {

				@Override
				public void run() {
					// 构造PayTask 对象
					PayTask alipay = new PayTask(cordova.getActivity());
					// 调用支付接口
					String result = alipay.pay(payInfo,true);

					Message msg = new Message();
					msg.what = SDK_PAY_FLAG;
					msg.obj = result;
					mHandler.sendMessage(msg);
				}
			};

			Thread payThread = new Thread(payRunnable);
			payThread.start();


		} catch (JSONException e1) {
			e1.printStackTrace();
			currentCallbackContext.error("订单参数不正确");
		}


		return true;
	}

	/**
	 * check whether the device has authentication alipay account.
	 * 查询终端设备是否存在支付宝认证账户
	 *
	 */
	public void check(View v) {
		Runnable checkRunnable = new Runnable() {

			@Override
			public void run() {
				PayTask payTask = new PayTask(cordova.getActivity());
				boolean isExist = true;

				Message msg = new Message();
				msg.what = SDK_CHECK_FLAG;
				msg.obj = isExist;
				mHandler.sendMessage(msg);
			}
		};

		Thread checkThread = new Thread(checkRunnable);
		checkThread.start();

	}

	/**
	 * get the sdk version. 获取SDK版本号
	 *
	 */
	public void getSDKVersion() {
		PayTask payTask = new PayTask(cordova.getActivity());
		String version = payTask.getVersion();
		Toast.makeText(cordova.getActivity(), version, Toast.LENGTH_SHORT).show();
	}

	/**
	 * create the order info. 创建订单信息
	 *
	 */
	public String getOrderInfo(String seller,String subject, String body, String price,String tradeNo,String timeout,String notifyUrl) {
		// 合作者身份ID
		String orderInfo = "partner=" + "\"" + partner + "\"";

		// 卖家支付宝账号
		orderInfo += "&seller_id=" + "\"" + seller + "\"";

		// 商户网站唯一订单号
		orderInfo += "&out_trade_no=" + "\"" + tradeNo + "\"";

		// 商品名称
		orderInfo += "&subject=" + "\"" + subject + "\"";

		// 商品详情
		orderInfo += "&body=" + "\"" + body + "\"";

		// 商品金额
		orderInfo += "&total_fee=" + "\"" + price + "\"";

		// 服务器异步通知页面路径
		orderInfo += "&notify_url=" + "\"" + notifyUrl
				+ "\"";

		// 接口名称， 固定值
		orderInfo += "&service=\"mobile.securitypay.pay\"";

		// 支付类型， 固定值
		orderInfo += "&payment_type=\"1\"";

		// 参数编码， 固定值
		orderInfo += "&_input_charset=\"utf-8\"";

		// 设置未付款交易的超时时间
		// 默认30分钟，一旦超时，该笔交易就会自动被关闭。
		// 取值范围：1m～15d。
		// m-分钟，h-小时，d-天，1c-当天（无论交易何时创建，都在0点关闭）。
		// 该参数数值不接受小数点，如1.5h，可转换为90m。
		orderInfo += "&it_b_pay=\""+timeout+"\"";

		// 支付宝处理完请求后，当前页面跳转到商户指定页面的路径，可空
		orderInfo += "&return_url=\"m.alipay.com\"";

		// 调用银行卡支付，需配置此参数，参与签名， 固定值
		// orderInfo += "&paymethod=\"expressGateway\"";

		return orderInfo;
	}


	/**
	 * sign the order info. 对订单信息进行签名
	 *
	 * @param content
	 *            待签名订单信息
	 */
	public String sign(String content) {
		return SignUtils.sign(content, rsa_private);
	}

	/**
	 * get the sign type we use. 获取签名方式
	 *
	 */
	public String getSignType() {
		return "sign_type=\"RSA\"";
	}

	private boolean qrpay(CordovaArgs args) {
		JSONObject biz_content = null;
		try {
			JSONObject orderInfoArgs =  args.getJSONObject(0);

			biz_content = orderInfoArgs.getJSONObject("biz_content");

			appid = orderInfoArgs.optString("appid", "");
			rsa_private = orderInfoArgs.optString("rsa_private", "");
			rsa_public = orderInfoArgs.optString("rsa_public", "");
			gateway = orderInfoArgs.optString("gateway",GATEWAY);

		} catch(JSONException err) {
			err.printStackTrace();
			currentCallbackContext.error("订单参数不正确");
		}

    	final AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//    	request.setBizContent("{\"out_trade_no\":\"tradeprecreate14841419873136814759\",\"seller_id\":\"\",\"total_amount\":\"0.01\",\"undiscountable_amount\":\"0\",\"subject\":\"xxx品牌xxx门店当面付扫码消费\",\"body\":\"购买商品3件共20.00元\",\"goods_detail\":[{\"goods_id\":\"goods_id001\",\"goods_name\":\"xxx小面包\",\"quantity\":1,\"price\":\"10\"},{\"goods_id\":\"goods_id002\",\"goods_name\":\"xxx牙刷\",\"quantity\":2,\"price\":\"5\"}],\"operator_id\":\"test_operator_id\",\"store_id\":\"test_store_id\",\"extend_params\":{\"sys_service_provider_id\":\"2088100200300400500\"},\"timeout_express\":\"120m\"}");
    	request.setBizContent(biz_content.toString());


		Runnable qrPayRunnable = new Runnable() {

			@Override
			public void run() {
				// 调用支付接口
				CustomAlipayClient alipayClient = new CustomAlipayClient(gateway,appid,rsa_private,format,charset,rsa_public,signtype);

				CustomAlipayTradePrecreateResponse response = new CustomAlipayTradePrecreateResponse();
		    	try
		    	{
		    		response = getAlipayTradePrecreateResponse(alipayClient.execute(request));
		    	} catch(AlipayApiException err) {
		    		err.printStackTrace();
		    	}

				Message msg = new Message();
				msg.what = SDK_QRPAY_FLAG;
				try {
					msg.obj = response.parsedJson();
				} catch(JSONException err) {
					err.printStackTrace();
					currentCallbackContext.error("支付宝预下单，序列化失败");
				}

				mHandler.sendMessage(msg);
			}
		};

		Thread qrPayThread = new Thread(qrPayRunnable);
    qrPayThread.start();

		return true;
	}

	private CustomAlipayTradePrecreateResponse getAlipayTradePrecreateResponse(String jsonStr) {
		IParseResponse rsp = new CustomAlipayTradePrecreateResponse();
		try {
			rsp.parsedResponse(jsonStr);
		} catch (JSONException e1) {
			e1.printStackTrace();
			currentCallbackContext.error("支付宝预下单，反序列化失败");
		}
		return (CustomAlipayTradePrecreateResponse)rsp;
	}


  /**
   * 统一收单线下交易查询
   * @param args
   * @return
     */
  private boolean tradeQuery(CordovaArgs args) throws  JSONException{
     JSONObject tradeQueryInfoArgs =  args.getJSONObject(0);

    appid = tradeQueryInfoArgs.optString("appid", "");
    rsa_private = tradeQueryInfoArgs.optString("rsa_private", "");
    rsa_public = tradeQueryInfoArgs.optString("rsa_public", "");
    gateway = tradeQueryInfoArgs.optString("gateway",GATEWAY);
    outTradeNo = tradeQueryInfoArgs.optString("out_trade_no","");
    tradeNo = tradeQueryInfoArgs.optString("trade_no","");


    Runnable tradeQueryRunnable = new Runnable() {

      @Override
      public void run() {
        // 调用统一收单线下交易查询接口
        CustomAlipayClient alipayClient = new CustomAlipayClient(gateway,appid,rsa_private,format,charset,rsa_public,signtype);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent(String.format("{" +
          "    \"out_trade_no\":\"%s\"," +
          "    \"trade_no\":\"%s\"" +
          "  }",outTradeNo,tradeNo));
        String response = "";
        try {
          response = alipayClient.execute(request);
        }  catch (AlipayApiException err) {
          currentCallbackContext.error("调用统一收单线下交易查询接口失败:" + err.getErrMsg());
        }

        Message msg = new Message();
//        Gson gs = new Gson();
        msg.what = SDK_TRADE_QUERY_FLAG;
//        msg.obj = gs.toJson(response);
        msg.obj = response;
        mHandler.sendMessage(msg);

      }
    };

    Thread tradeQueryThread = new Thread(tradeQueryRunnable);
    tradeQueryThread.start();

     return true;
  }


}
